<style>
  .error {color : red}
</style>
<div class="container-fluid p-t-10">
  <ol class="breadcrumb" style="font-weight: 700;margin-bottom: 10px">
    <li><a href="#">角色管理</a></li>
    <li class="active">选择权限组</li>
  </ol>
  <div class="row">
    <div class="col-lg-12">
      <div class="card">

        <div class="card-body">
          <h1>选择权限组</h1>
          <form id="add-form" action="" method="post" class="row">
            <div class="form-group col-md-12 {$data? '':'hide'}">
              <label for="id">角色编号</label>
              <input type="text" class="form-control" id="id" name="id" readonly value="{$data? $data.id:''}"/>
            </div>

            <div class="form-group col-md-12">
              <label for="role_name">角色名称</label>
              <input type="text" class="form-control" id="role_name" readonly name="role_name" value="{$data? $data.role_name:''}" placeholder="请输入角色名称 例如: 角色管理" />
            </div>

            <div class="form-group col-md-8">
                <label >权限组选择</label>
                <br>
                {foreach $groups as $vo}
                <label class="lyear-checkbox checkbox-inline checkbox-pink">
                  <input type="checkbox" value="{$vo.id}" {$vo.checked ? 'checked' : ''}><span>{$vo.group_name}</span>
                </label>
                {/foreach}

            </div>


          </form>
        </div>
      </div>
    </div>
  </div>
</div>
<script>

  $(function(){
      var roleId = '{$data.id}';
      $('input[type="checkbox"]').click(function(){
          var group_id = $(this).val();
          if($(this).is(":checked")){
              $._ajax({
                  url: '/role/addGroup',
                  data: {
                      id:roleId,
                      group_id:group_id,
                      type:'add'
                  }
              }).done(function(data){
                  if(data){
                      lightyear.notify('保存成功~', 'success');
                  }else{
                      lightyear.notify('操作失败~', 'danger');
                      setTimeout(function() {
                          window.location.reload();
                      }, 1e3);
                  }
              });
          }else{
              $._ajax({
                  url: '/role/addGroup',
                  data: {
                      id:roleId,
                      group_id:group_id,
                      type:'del'
                  }
              }).done(function(data){
                  if(data){
                      lightyear.notify('保存成功~', 'success');
                  }else{
                      lightyear.notify('操作失败~', 'danger');
                      setTimeout(function() {
                          window.location.reload();
                      }, 1e3);
                  }
              });
          }
      });


  });
</script>


